linux 进程优先级 之设置实时进程 (另一种方式是设置nice值) |
您所在的位置:网站首页 › 线程优先级设置为实时 win10 › linux 进程优先级 之设置实时进程 (另一种方式是设置nice值) |
Linux内核的三种调度策略: 1,SCHED_OTHER 分时调度策略, 2,SCHED_FIFO实时调度策略,先到先服务。一旦占用cpu则一直运行。一直运行直到有更高优先级任务到达或自己放弃 3,SCHED_RR实时调度策略,时间片轮转。当进程的时间片用完,系统将重新分配时间片,并置于就绪队列尾。放在队列尾保证了所有具有相同优先级的RR任务的调度公平 Linux线程优先级设置 首先,可以通过以下两个函数来获得线程可以设置的最高和最低优先级,函数中的策略即上述三种策略的宏定义:int sched_get_priority_max(int policy); int sched_get_priority_min(int policy); SCHED_OTHER是不支持优先级使用的,而SCHED_FIFO和SCHED_RR支持优先级的使用,他们分别为1和99,数值越大优先级越高。设置和获取优先级通过以下两个函数 int pthread_attr_setschedparam(pthread_attr_t *attr, const struct sched_param *param); int pthread_attr_getschedparam(const pthread_attr_t *attr, struct sched_param *param); param.sched_priority = 51; //设置优先级 系统创建线程时,默认的线程是SCHED_OTHER。所以如果我们要改变线程的调度策略的话,可以通过下面的这个函数实现。int pthread_attr_setschedpolicy(pthread_attr_t *attr, int policy); 上面的param使用了下面的这个数据结构:struct sched_param{ int __sched_priority; //所要设定的线程优先级}; 我们可以通过下面的测试程序来说明,我们自己使用的系统的支持的优先级:
#include #include #include #include static int get_thread_policy(pthread_attr_t *attr){ int policy; int rs = pthread_attr_getschedpolicy(attr,&policy); assert(rs==0); switch(policy) { case SCHED_FIFO: printf("policy= SCHED_FIFO\n"); break; case SCHED_RR: printf("policy= SCHED_RR"); break; case SCHED_OTHER: printf("policy=SCHED_OTHER\n"); break; default: printf("policy=UNKNOWN\n"); break; } return policy;} static void show_thread_priority(pthread_attr_t *attr,int policy){ int priority = sched_get_priority_max(policy); assert(priority!=-1); printf("max_priority=%d\n",priority); priority= sched_get_priority_min(policy); assert(priority!=-1); printf("min_priority=%d\n",priority);} static int get_thread_priority(pthread_attr_t *attr){ struct sched_param param; int rs = pthread_attr_getschedparam(attr,¶m); assert(rs==0); printf("priority=%d",param.__sched_priority); return param.__sched_priority;} static void set_thread_policy(pthread_attr_t *attr,int policy){ int rs = pthread_attr_setschedpolicy(attr,policy); assert(rs==0); get_thread_policy(attr);} int main(void){ pthread_attr_t attr; struct sched_param sched; int rs; rs = pthread_attr_init(&attr); assert(rs==0); int policy = get_thread_policy(&attr); printf("Show current configuration of priority\n"); show_thread_priority(&attr,policy); printf("show SCHED_FIFO of priority\n"); show_thread_priority(&attr,SCHED_FIFO); printf("show SCHED_RR of priority\n"); show_thread_priority(&attr,SCHED_RR); printf("show priority of current thread\n"); int priority = get_thread_priority(&attr); printf("Set thread policy\n"); printf("set SCHED_FIFO policy\n"); set_thread_policy(&attr,SCHED_FIFO); printf("set SCHED_RR policy\n"); set_thread_policy(&attr,SCHED_RR); printf("Restore current policy\n"); set_thread_policy(&attr,policy); rs = pthread_attr_destroy(&attr); assert(rs==0); return 0;} 下面是测试程序的运行结果:policy=SCHED_OTHERShow current configuration of prioritymax_priority=0min_priority=0show SCHED_FIFO of prioritymax_priority=99min_priority=1show SCHED_RR of prioritymax_priority=99min_priority=1show priority of current threadpriority=0Set thread policyset SCHED_FIFO policypolicy= SCHED_FIFOset SCHED_RR policypolicy= SCHED_RRRestore current policypolicy=SCHED_OTHER 这里测试一下其中的两种特性,SCHED_OTHER和SCHED_RR,还有就是优先级的问题,是不是能够保证,高优先级的线程,就可以保证先运行。 下面的这个测试程序,创建了三个线程,默认创建的线程的调度策略是SCHED_OTHER,其余的两个线程的调度策略设置成SCHED_RR。我的Linux的内核版本是2.6.31。SCHED_RR是根据时间片来确定线程的调度。时间片用完了,不管这个线程的优先级有多高都不会在运行,而是进入就绪队列中,等待下一个时间片的到了,那这个时间片到底要持续多长时间?在《深入理解Linux内核》中的第七章进程调度中,是这样描诉的,Linux采取单凭经验的方法,即选择尽可能长、同时能保持良好相应时间的一个时间片。这里也没有给出一个具体的时间来,可能会根据不同的CPU 来定,还有就是多CPU 的情况。 #include #include #include #include void Thread1(){ sleep(1); int i,j; int policy; struct sched_param param; pthread_getschedparam(pthread_self(),&policy,¶m); if(policy == SCHED_OTHER) printf("SCHED_OTHER\n"); if(policy == SCHED_RR); printf("SCHED_RR 1 \n"); if(policy==SCHED_FIFO) printf("SCHED_FIFO\n"); for(i=1;i |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |